package org.example.cglib.fastClass;

import org.example.cglib.A;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.reflect.FastClass;

import java.lang.reflect.Method;

/**
 * @author : yuhuan.wu
 * @date : 2021/11/9
 * @description :
 */
public class FastClassTest {


    public void testFastClass() throws Exception {
        A a = new A();
        a.setDa("duo");
        a.setShao(1);

        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\cglib");

        long start = System.currentTimeMillis();
        FastClass fastA = FastClass.create(A.class);
//        for (int i = 0; i < 10000000; i++) {
            fastA.invoke("getShao", Constants.EMPTY_CLASS_ARRAY, a, new Object[]{});
//        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    /*
     * 耗时
     * 64352651
     * 61022533
     * 62674898
     * 96095258
     * 71985404
     *
     * -Dsun.reflect.inflationThreshold=2147483647 耗时
     * 2007548478
     * 2283596315
     * 2057156791
     * 2109367771
     * 2050146054
     */
    public void testReflex() throws Exception {
        A a = new A();
        a.setDa("duo");
        a.setShao(1);

        long start = System.currentTimeMillis();
        Method getShao = A.class.getMethod("getShao");
//        for (int i = 0; i < 10000000; i++) {
            getShao.invoke(a);
//        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    public static void main(String[] args) throws Exception {
        //132850900  148
        new FastClassTest().testFastClass();

        //26141100  26
//        new FastClassTest().testReflex();

    }


}
